package com.mock.ems.redis;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.Tuple;

import java.util.List;
import java.util.Set;

public class RedisClusterHandler implements RedisHandler{
	
		private static JedisCluster redisCluster;
		
		private static final Logger logger = LoggerFactory.getLogger(RedisHandler.class);
		
		public Long zadd(String key,Double score,String member) {
			return redisCluster.zadd(key, score, member);
		}
		
		public void zremrangeByRank(String key,Long start,Long end) {
			redisCluster.zremrangeByRank(key, start, end);
		}
		
		public void zincrby(String key,Double score,String member) {
			redisCluster.zincrby(key, score, member);
		}
		
		public Long zcard(String key) {
			return redisCluster.zcard(key);
		}
		
		public Set<String> zrange(String key,Long start,Long end) {
			return redisCluster.zrange(key, start, end);
		}
		
		public Set<String> zrevrange(String key,Long start,Long end) {
			return redisCluster.zrevrange(key, start, end);
		}
		
		public Long zrank(String key,String member) {
			return redisCluster.zrank(key, member);
		}
		
		public Long zrevrank(String key,String member) {
			return redisCluster.zrevrank(key, member);
		}
		
		
		/** 
		* @Title: zrevrangeWithScores 
		* @Description: 获取ZSET中的Score和Member
		* @param key
		* @param start
		* @param end
		* @return Set<Tuple>
		* @throws 
		*/
		public Set<Tuple> zrevrangeWithScores(String key,Long start,Long end) {
			return redisCluster.zrevrangeWithScores(key, start, end);
		}
		
		public Double zscore(String key,String member) {
			return redisCluster.zscore(key, member);
		}
		
		public Long zrem(String key, String ...member){
			return redisCluster.zrem(key, member);
		}
		
		public boolean set(String key,Object val) {
			String statusCode = redisCluster.set(key, JSON.toJSONString(val));
			return statusCode.equals("OK");
		}
		
		public boolean del(String key) {
			return redisCluster.del(key) > 0;
		}
		
		public Boolean set(String key,String val,Integer timeToLive) {
			String statusCode = redisCluster.set(key, val);
			Long expired = redisCluster.expire(key, timeToLive);
			logger.info("statusCode:{},expired:{},key:{},timeToLive:{}",new Object[]{statusCode,expired,key,timeToLive});
			return statusCode.equals("OK") && expired == 1;
		}
		
		public Boolean set(String key,Object val,Integer timeToLive) {
			String statusCode = redisCluster.set(key, JSON.toJSONString(val));
			Long expired = redisCluster.expire(key, timeToLive);
			return statusCode.equals("OK") && expired == 1;
		}
		
		public <T> T get(String key,Class<T> clazz) {
			String value = redisCluster.get(key);
			if (null == value) return null;
			return JSON.parseObject(value, clazz);
		}
		
		public Long incr(String key ,Long offset) {
			return redisCluster.incrBy(key, offset);
		}
		
		public Long decr(String key ,Long offset) {
			return redisCluster.decrBy(key, offset);
		}
		
		public Long ttl(String key) {
			return redisCluster.ttl(key);
		}
		
		public boolean expiredAt(String key,Long unixTime) {
			return redisCluster.expireAt(key, unixTime) > 1;
		}
		
		public boolean expired(String key,Integer timeToLive) {
			return redisCluster.expire(key, timeToLive) > 0;
		}
		
		public Long rpush(String key,String ...elements) {
			return redisCluster.rpush(key, elements);
		}
		
		public List<String> lrange(String key,Long start,Long end) {
			return redisCluster.lrange(key, start, end);
		}
		
		public Long lrem(String key, String element){
			return redisCluster.lrem(key, 1L, element);
		}
		
		public boolean setnx(String key,String value) {
			return redisCluster.setnx(key, value) == 1;
		}
		
		public boolean hset(String key,String field,String value) {
			Long result = redisCluster.hset(key, field, value);
			return (result == 0 || result ==  1);
		}
		
		public Long hincrby(String key,String field,Long value) {
			return redisCluster.hincrBy(key, field, value);
		}
		
		public Double hincrbyfloat(String key,String field,Double value) {
			return redisCluster.hincrByFloat(key, field, value);
		}
		
		public boolean hdel(String key,String ...fields) {
			Long result = redisCluster.hdel(key, fields);
			return (result > 0);
		}
		
		public String hget(String key,String field) {
			return redisCluster.hget(key, field);
		}
		
		public List<String> hmget(String key,String ...fields) {
			return redisCluster.hmget(key, fields);
		}
		
		public Set<String> hkeys(String key) {
			return redisCluster.hkeys(key);
		}
		
		public List<String> hvals(String key) {
			return redisCluster.hvals(key);
		}
		
		public List<String> blpop(int timeout,String key) {
			return redisCluster.blpop(timeout, key);
		}

		@Override
		public Long publish(String channel, String message) {
			return redisCluster.publish(channel, message);
		}

		@Override
		public void subscribe(JedisPubSub jedisPubSub,String... channels) {
			redisCluster.subscribe(jedisPubSub, channels);
		}

		@Override
		public Double incrByFloat(String key, Double value) {
			return redisCluster.incrByFloat(key, value);
		}

		@Override
		public Set<String> zrangeByScore(String key,double min,double max) {
			return redisCluster.zrangeByScore(key, min, max);
		}
		
}
